package com.tvunetworks.center.user.controller.api;

import com.tvunetworks.center.common.model.LoginUser;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import com.tvunetworks.center.user.model.User;
import com.tvunetworks.center.user.model.param.api.AddExtParam;
import com.tvunetworks.center.user.model.param.api.UpdateExtParam;
import com.tvunetworks.center.user.service.ExtService;
import com.tvunetworks.center.user.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @program: tvucc-aggregator
 * @description: UserUniformApiController
 * @author lebronchen
 * @create: 2020-03-11 14:32
 **/
@RestController
@RequestMapping("/uniformapi/${openApiVersion}/user")
public class UserUniformApiController {

    @Autowired
    private ExtService extService;
    @Autowired
    private UserService userService;

    /**
     *
     * @param param param
     * @return Result
     */
    @PostMapping("/addExt")
    public Result addExt(@Valid @RequestBody AddExtParam param){
        User user = userService.getByEmail(param.getEmail());
        if (user == null) {
            return ResultUtil.error("81000102", "User not exist");
        }
        LoginUser loginUser = new LoginUser();
        BeanUtils.copyProperties(user, loginUser);
        return extService.addExtToUser(loginUser, param.getName(), param.getUrl(), param.getType(), param.getRid(),null);
    }

    /**
     *
     * @param param param
     * @return Result
     */
    @PostMapping("/updateExt")
    public Result updateExt(@Valid @RequestBody UpdateExtParam param){
        return extService.updateExt(param.getPeerId(), param.getName(), param.getUrl(),null);
    }
}
